package com.wolfsea.rvitemdecorationdemo.itemdecoration
import android.content.Context
import android.graphics.*
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.wolfsea.rvitemdecorationdemo.R

/**
 *@desc  时间轴ItemDecoration
 *@author liuliheng
 *@time 2022/3/16  12:02
 **/
class HomeTimeLineItemDecoration(context: Context) : RecyclerView.ItemDecoration() {

    private val mContext = context
    private var paint: Paint = Paint()
    private var mUsLinePaint: Paint = Paint()
    private var mSLinePaint: Paint = Paint()

    private var mBitmap: Bitmap

    private var mLeft = 0
    private var currentIndex = -1

    init {
        paint.isAntiAlias = true
        paint.isDither = true
        paint.textSize = mContext.resources.getDimension(R.dimen.sp_16)
        paint.color = mContext.resources.getColor(R.color.purple_200)

        mUsLinePaint.isAntiAlias = true
        mUsLinePaint.isDither = true
        mUsLinePaint.strokeWidth = mContext.resources.getDimension(R.dimen.dp_1)
        mUsLinePaint.color = mContext.resources.getColor(R.color.red)

        mSLinePaint.isAntiAlias = true
        mSLinePaint.isDither = true
        mSLinePaint.strokeWidth = mContext.resources.getDimension(R.dimen.dp_1)
        mSLinePaint.color = Color.GREEN

        mLeft = mContext.resources.getDimensionPixelOffset(R.dimen.dp_75)

        mBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.icon_ok)
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {

        super.onDraw(canvas, parent, state)

        val itemCount = parent.childCount
        Log.d("data001", "itemCount:${itemCount}")

        for (i in 0.until(itemCount)) {

            val child = parent.getChildAt(i)
            val height = child.measuredHeight
            val text = "序号:${i + 1}"

            val tStartX0 = mLeft.toFloat() - 20
            val tStartY0 = height.times(i).toFloat()
            val tStartX1 = tStartX0
            val tStartY1 = tStartY0 + height.div(2)

            val linePaint = if (i <= currentIndex) mSLinePaint else mUsLinePaint
            canvas.drawLine(tStartX0, tStartY0, tStartX1, tStartY1, linePaint)

            canvas.drawText(text, mLeft.div(4F), height.div(2.0F) + height * i + 12, paint)

            val uStartX0 = mLeft.toFloat() - 20
            val uStartY0 = height.div(2F) + height.times(i)
            val uStartX1 = uStartX0
            val uStartY1 = uStartY0 + height.div(2)
            canvas.drawLine(uStartX0, tStartY0, uStartX1, uStartY1, linePaint)

            val mLeft = mLeft.toFloat() - 20 - (mBitmap.width shr 1)
            val mTop = height.div(2F) + height.times(i) - (mBitmap.height shr 1)
            canvas.drawBitmap(mBitmap, mLeft, mTop, null)
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.set(mLeft, 0, 0, 0)
    }

    fun updateCurrentIndex(index: Int) {
        this.currentIndex = index
    }

}